/**
 * Created by caelan on 2018/10/23.
 */
var drySetting = {
  /*指标设置数据，重置使用*/
  indexData: [],
  /*发送请求的数据，以及修改的数据*/
  postData: [],
  html: function (indexArray) {
    this.indexData = JSON.parse(JSON.stringify(indexArray));
    this.postData = JSON.parse(JSON.stringify(indexArray));
    var html = '';

    indexArray.forEach(function (val, ind) {
      html += '<div class="layui-form-item pluginsSingleRows">' +
        '<label class="layui-form-label">'+ val.name +':</label>' +
        '<div class="layui-input-block">' +
          '<button data-ind="'+ ind +'" class="index-setting-btn layui-btn layui-btn-xs layui-btn-normal">指标设置</button>' +
        '</div>' +
      '</div>';
    });

    return '<div class="pluginsDiv" id="drySetting">'+ html +'</div>';
  },
  getData: function () {


    /*参数获取需要按照数据顺序来，没有设key值
    从苗期到乳熟期，从轻到重，既符合页面视觉，也符合数据视图，数据获取也简单，
    修改成其他内容也需如此，或者修改一下数据结构*/

    var postString = this.postData.map(function (val) {
      var name=val.name;
      return 'DayHighTempL=' + val.stage[0].range[0].value[0] + ';' + 'DayHighTempL=' + val.stage[0].range[0].value[1] + ';' +
        'TimeHumidityL=' + val.stage[0].range[1].value[0] + ';' + 'TimeHumidityL=' + val.stage[0].range[1].value[1] + ';' +
        'TimeWindL=' + val.stage[0].range[2].value[0] + ';' + 'TimeWindL=' + val.stage[0].range[2].value[1] + ';' +
        'DayHighTempH=' + val.stage[1].range[0].value[0] + ';' + 'DayHighTempH=' + val.stage[1].range[0].value[1] + ';' +
        'TimeHumidityH=' + val.stage[1].range[1].value[0] + ';' + 'TimeHumidityH=' + val.stage[1].range[1].value[1] + ';' +
        'TimeWindH=' + val.stage[1].range[2].value[0] + ';' + 'TimeWindH=' + val.stage[1].range[2].value[1] ;

    }).join('');


    Global.postData.paramsMap += ';' + postString;
  },
  renderCallback: function () {
    var _this = this;
    $('#drySetting').find('.index-setting-btn').click(function () {

      function getHtml(data){
        return data.map(function (cur) {
          var range = cur.range.map(function (val) {
            return '<div class="range">' +
              '<input step="0.1" style="width:25%" class="min layui-input layui-input-inline" value="'+ Number(val.value[0]).toFixed(1) +'" type="number">' +
              ' <= <span class="name"> '+ val.name +' </span> &lt; ' +
              '<input step="0.1" style="width:25%" class="max layui-input layui-input-inline" value="'+ Number(val.value[1]).toFixed(1) +'" type="number">' +
              '</div>';
          }).join('');
          return '<div class="stage"><h5 class="title">'+ cur.name +'</h5>'+ range +'</div>';
        }).join('');
      }

      var curInd = $(this).attr('data-ind');
      var openHtml = getHtml(_this.postData[curInd].stage);

      layLayer.open({
        type: 1,
        title: '等级指标设置',
        area: '300px',
        btn: ['确定', '重置'],
        btnAlign: 'c',
        content: '<div class="index-setting-layer">'+ openHtml +'</div>',
        /*yes是按钮一的回调，第二个按钮是btn2*/
        yes: function (ind) {
          /*修改赋值给postdata*/
          _this.postData[curInd].stage =  [].slice.call($('.index-setting-layer').children('.stage')).map(function (val) {
            var range = [].slice.call($(val).children('.range')).map(function (ran) {
              return {name: $(ran).children('.name').text(), value: [$(ran).children('.min').val(), $(ran).children('.max').val()]};
            });
            return {name: $(val).children('.title').text(), range: range};
          });

          layLayer.close(ind);
        },
        /*重置*/
        btn2: function () {
          $('.index-setting-layer').html(getHtml(_this.indexData[curInd].stage));
          return false;     //禁止点击该按钮关闭
        }
      });
    });
  }
};